package springboot.example.domain;

import org.json.JSONArray;
import org.json.JSONObject;
import springboot.example.entity.KeyValue;

import java.util.List;

public class HandleResultData <T>{

   private List<T> result;

    public HandleResultData(List<T> result) {
        this.result = result;

    }

    public JSONArray toJsonArray(){
        if (result == null) {
            return null;
        }
        JSONArray data = new JSONArray();
        result.stream().forEach( t -> data.put(new JSONObject(t)));
        return data;
    }

    public JSONObject toJsonObject(){
        if (result == null) {
            return null;
        }
        JSONObject data = new JSONObject();
        result.stream().forEach(keyValue -> data.put(((KeyValue)keyValue).getKey(), ((KeyValue)keyValue).getValue()));
        return data;
    }

}
